/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function loginWeb()
{
  //var request = new XMLHttpRequest();  
  //request.open('GET', 'http://172.16.50.171/emailshow', true);
  //
  var loginUser=document.getElementById("UName").value;
  var loginPass=document.getElementById("PWord").value;
  var newpass = encryptpwd(loginPass);
  //alert(loginUser);
  //alert(loginPass);
  //alert(url);
  var url="http://172.16.50.185/cloud/tb/tb_reg.php?username="+loginUser+"&password="+newpass;
  //alert(url);
  var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
  if(req)
  {    
   req.open('GET',url, true);  
   req.send();
   req.onreadystatechange = doResult;
  }
  //alert(req.readyState);
  function doResult()
{
  //alert(req.responseText);
  if(req.readyState == 4) {
  if(req.status == 200) {
    // alert(req.responseText);
      var flag=req.responseText;
      if(flag!=="fail2"&&flag!=="fail"&&flag!=="fail1")
      {     
       //alert(req.responseText);   
       Components.utils.import("resource://gre/modules/NetUtil.jsm");
       Components.utils.import("resource://gre/modules/FileUtils.jsm");
       var loginfile = FileUtils.getFile("ProfD", ["login.xml"]);
       if(loginfile.exists())
       {
         loginfile.remove(true);
       }
      var dirService = Components.classes["@mozilla.org/file/directory_service;1"].
          getService(Components.interfaces.nsIProperties);
      var dir = dirService.get("ProfD", Components.interfaces.nsIFile);
      dir.append("login.xml");
      var ostream = FileUtils.openSafeFileOutputStream(dir);

      var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
                createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
      converter.charset = "UTF-8";
      var xmlIdentify="<?xml version='1.0' encoding='utf-8' standalone='yes' ?>";
      var xmlhead="<login>";
      var xmlend="<"+'\/'+"login>";
      var xmlcontent="";
      xmlcontent=xmlcontent+"<user>"+loginUser+"<"+'\/'+"user>" +"<password>"+loginPass+"<"+'\/'+"password>";   
      var data=xmlIdentify+xmlhead+xmlcontent+xmlend;
      var istream = converter.convertToInputStream(data);  
      NetUtil.asyncCopy(istream, ostream, function(status){
      if (!Components.isSuccessCode(status)) {
      alert("写入数据错误");
      return;
      }else{
      }
      });   
      var url=req.responseText;
      openTab("contentTab", {contentPage: url}, null);
      window.close();}else{
      //alert(req.responseText);
      var hboxObj=document.getElementById("infohbox");
      var info=document.getElementById("outputinfo");
      hboxObj.setAttribute("hidden",false);
  }
  }
  }      
}
}


function registerUser(url)
{
   openTab("contentTab", {contentPage: url}, null); 
   window.close();
}

function encryptpwd(password)
{
    var key_hash = CryptoJS.MD5("encryption_key"); 
    var key = CryptoJS.enc.Utf8.parse(key_hash); 
    var iv  = CryptoJS.enc.Utf8.parse('1234567812345678'); 
    var encrypted = CryptoJS.AES.encrypt(password, key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}); 
    return encrypted;
}